package com.alibaba.middleware.race.sync;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sync.utils.Message;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * Created by Mark Hsiu
 */
public class NettySeverHandler extends ChannelInboundHandlerAdapter {
	
	private static final Logger logger = LoggerFactory.getLogger(Server.class);
	
	
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {   	
        Message message = (Message)msg;
        this.handler(ctx,message);
    }
    
    private void handler(ChannelHandlerContext ctx, Message message){
    	 if(message.getStatus() == Message.connect){
    		 Server.channelMap.put("client", ctx.channel());
    		 logger.info(" connect  client .. ");   		 
    	 }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext channel, Throwable cause) {
        cause.printStackTrace();
        channel.close();
    }
}
